package com.nd.rj.common.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

public class ComfunHelp {
    
    /**
     * @Title: getDeviceId  
     * @Description: TODO(获取设备号)  
     * @author yanyy
     * @date 2012-8-31 上午09:40:35 
     *
     * @return       
     * @return String
     * @throws
     */
    public static String getDeviceId(Context context) {
        //直接从配置文件读取
        SharedPreferences settings = context.getSharedPreferences("deviceId", 0);
        String deviceId = settings.getString("deviceId", null);
        if (TextUtils.isEmpty(deviceId)) {
            try {
                TelephonyManager tm = (TelephonyManager) context
                        .getSystemService(Context.TELEPHONY_SERVICE);
                deviceId = tm.getDeviceId();
            } catch (Exception e) {

            }
            if (TextUtils.isEmpty(deviceId)) {
                // 有些机器有可能取不到设备号用GUID表示
                deviceId = UUID.randomUUID().toString().replace("-", "");
            }
            // 保存到配置文件(这样可以保证每次都一样)
            SharedPreferences.Editor editor = settings.edit();
            editor.putString("deviceId", deviceId);
            editor.commit();
        }

        return deviceId;
    }

    
    /**
     * md5加密函数
     * 
     * @param aSrc 被加密串
     * @return
     */
    public static String Md5Digest(String aSrc, boolean aUpperCase) {
        String sResult = "";
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(aSrc.getBytes());
            byte b[] = md5.digest();
            StringBuffer buf = new StringBuffer("");
            // 转16进制
            for (int offset = 0; offset < b.length; offset++) {
                int i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            sResult = buf.toString();
            if (aUpperCase) {
                sResult = sResult.toUpperCase();
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return sResult;
    }

    public static String Md5Digest(String aSrc) {
        return Md5Digest(aSrc, true);
    }
    
    /**
     * @brief 【返回当前网络是否有效】
     * @n<b>函数名称</b>     :isNetworkAvailable
     * @param context
     * @return
     * @return    boolean   
     * @<b>作者</b>          :  陈希
     * @<b>修改</b>          :
     * @<b>创建时间</b>      :  2012-5-18上午10:00:50
     * @<b>修改时间</b>      :
    */
    public static boolean isNetworkAvailable(Context context) {
        return getActiveNetWorkName(context) != null;
    }

    /**
     * @brief 【返回当前有效网络名】
     * @n<b>函数名称</b>     :getActiveNetWorkName
     * @param context
     * @return
     * @return    String   
     * @<b>作者</b>          :  陈希
     * @<b>修改</b>          :
     * @<b>创建时间</b>      :  2012-5-18上午10:01:40
     * @<b>修改时间</b>      :
    */
    public static String getActiveNetWorkName(Context context) {
        context = context.getApplicationContext();
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        String result = null;

        if (connectivity == null) {
            return null;
        }

        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {

                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    result = info[i].getTypeName();
                }
            }
        }

        return result;
    }
}
